<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of channelmanager
 *
 * @author dragon.hu add @2016.5.3
 */
defined('BASEPATH') or die('forbidden');

class Channelmanager_Controller extends Controller {
    private $strategy_array;
    private $wholeServerHost;
    private $allchannels;
    private $tmpconnect;
    
    public function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        //$this->strategy_array = array('lastarea'=>  translate('GENERAL_STRATEGY_LASTAREA'),'randarea' => translate('GENERAL_STRATEGY_RANDAREA'));
        $config = get_config();
        $this->allchannels = $config['inc']['allchannel'];
        //var_dump($this->allchannels);
        //exit();
        $this->tmpconnect = $this->load->database('center_db') or die("load failed!".mysql_error());
        $this->wholeServerHost = $config['host_url'];
    }

    function index() {
        $this->view->display("general/channelmanager/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        
        $sql = "SELECT COUNT(*) AS num FROM channelmanager WHERE {$where}";
        $one = $this->tmpconnect->one($sql);
        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder}" : "";
        $this->tmpconnect->query("SELECT {$colsinfo} FROM channelmanager WHERE {$where} {$orderby} LIMIT {$offset},{$rp}");
        while ($n = $this->tmpconnect->get_array('default')) {
            $rows[] = array('id' => $n['id'], 'cell' => $n);
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

function add()
	{
		$tmparr = array();
		$sql = "SELECT * FROM channelmanager";
		$this->tmpconnect->query($sql);
		while($n = $this->tmpconnect->get_array('default'))
		{
			$tmparr[] = $n;
		}
		
		if (is_submit())
		{
			$rowdata = $_POST['row'];
			//var_dump($_POST);
			//exit();
			for ($i = 0; $i < count($tmparr); $i++)
			{
				if ($tmparr[$i]['channelname'] == $rowdata['channel'])
				{
					echo "<script>alert('channel can not repeat !!!');</script>";
					$this->view->assign ("channel", $this->form->select ("row[channel]", $this->allchannels, '', array('id' => 'channel')));
					$this->view->display("general/channelmanager/add");
					return;
				}
			}
			//vpost
			//$n['channel'] = $rowdata['channel'];
			//$n['operation'] = 1;
			//$jsonStr = json_encode($n);
			if ($rowdata['channel'] == 'shzmTest')
					$rowdata['channel'] = 'PCChannel';
			$result = vpost($this->wholeServerHost."channel=".$rowdata['channel']."&status=".$rowdata['status']."&operation=1"."&action=47");
			if ($rowdata['channel'] == 'PCChannel')
					$rowdata['channel'] = 'shzmTest';
			if ($result != '0' || $result === FALSE)
			{
				cp_tips("error");
			}
			else
			{
				//2.加入数据库中
				$channel = $this->allchannels[$rowdata['channel']];
				$manager = $this->auth->fields['username'];
				$addtime = date('Y-m-d H:i:s', time());
				$sql = "INSERT INTO channelmanager (channel, channelname, manager, status, addtime) VALUES ('$channel', '".$rowdata['channel']."', '".$manager."',".$rowdata['status'].", '".$addtime."')";
				if($this->tmpconnect->execute($sql))
				{
					insert_log(translate('MENU_GENERAL_CHANNEL'),translate('EDIT'), NULL, array('channel' => $rowdata['channel']));
					cp_tips("success");
				}
			}
		}
		$this->view->assign ("channel", $this->form->select ("row[channel]", $this->allchannels, '', array('id' => 'channel')));
		$this->view->display("general/channelmanager/add");
	}

	function del($id = 0)
	{ 
		$c = 0;
		$r = '';
		if ($id === 0)
			json_output($c, translate('C_INVALID_PARAM_ID', $id));
		$row = $this->tmpconnect->one("SELECT * FROM channelmanager WHERE id=$id");
		if (!is_array($row))
			json_output($c, translate('C_INVALID_PARAM_ID', $id));
		
		//vpost
		//$n['channel'] = $row['channelname'];
		//$n['operation'] = 0;
		//$jsonStr = json_encode($n);
		if ($row['channelname'] == 'shzmTest')
			$row['channelname'] = 'PCChannel';
		$result = vpost($this->wholeServerHost."channel=".$row['channelname']."&status=".$row['status']."&operation=0"."&action=47");
		if ($row['channelname'] == 'PCChannel')
			$row['channelname'] = 'shzmTest';
		if ($result != '0' || $result === FALSE)
		{
			$c = 1;
		}
		else
		{
			if ($this->tmpconnect->delete('channelmanager', array('id' => $id))) {
				insert_log(translate('MENU_GENERAL_CHANNEL'),translate('DEL'), array('id'=> $id));
				$c = 1;
			} else {
				$r = translate('C_TIPS_ERROR_TEXT');
			}
		}
		json_output($c, $r);
	}
    
}
